<?php
namespace app\controller;
class AccountController extends BaseController
{
    public function login()
    {
        if(session("member_id")){
            return redirect(url('Index/index'));
        }
        if(request()->isPost()){
            $account = input('account');
            $password = input('password');
            if ($account == '') {
                $this->error ( '请输入手机号码' );
            }
            if ($password == '') {
                $this->error ( '请输入密码' );
            }
            $this->sitedb->map = [];
            $this->sitedb->map_eq("mobile",$account);
            $info = $this->sitedb->find("member");
            /* 判断手机号码是否存在 */
            if(!$info['id']){
                return $this->error('账号不存在');
            }else{
                /* 判断密码是否正确 */
                if(md5(md5($password).$info['salt'].$info['login_time'])!=$info['password']){
                    return $this->error('密码错误');
                }else{
                    /** 更新登录时间和登录IP */
                    $salt=$this->stroperate->getRandChar(6);
                    $login_time=time();
                    $this->sitedb->data['login_time'] = $login_time;
                    $this->sitedb->data['salt'] = $salt;
                    $this->sitedb->data['password']=md5(md5($password).$salt.$login_time);
                    $this->sitedb->update("member");
                    session('member_id',$info['id']);
                    return $this->success('登录成功',"",url("Index/index"));
                }
            }
        }else{
            return view();
        }
    }
    public function register()
    {
        if(session("member_id")){
            return redirect(url('Index/index'));
        }
        if(request()->isPost()){
            /** 更新登录时间和登录IP */
            $salt=$this->stroperate->getRandChar(6);
            $login_time=time();
            $data['nickname'] = input('account');
            $data['mobile'] = input('mobile');
            $data['login_time'] = $login_time;
            $data['salt'] = $salt;
            $data['password']=input('password');
            $data['password_confirm']=input("password_confirm");
            /* 验证 -S */
            $validate=new \app\validate\Register;
            $check_result=$validate->check($data);
            if(!$check_result){
                return $this->error($validate->getError());
            }
            /* 验证 -E */
            $data['password']=md5(md5(input('password')).$salt.$login_time);
            $this->sitedb->data=$data;
            $result=$this->sitedb->update("member");
            session('member_id',$result);
            return $this->success("注册成功","",url("Index/index"));
        }else{
            return view();
        }
    }

    public function profile()
    {
        if(request()->isPost()){
            $this->sitedb->map_eq("id",session("member_id"));

            $data['nickname'] = input('account');
            $data['password'] = input('password');
            $data['password_confirm'] = input('password_confirm');

            /* 验证 -S */
            $validate=new \app\validate\Profile;
            $check_result=$validate->check($data);
            if(!$check_result){
                return $this->error($validate->getError());
            }
            /* 验证 -E */

            if($data['password']){
                $salt=$this->stroperate->getRandChar(6);
                $login_time=time();
                $data['password']=md5(md5($data['password']).$salt.$login_time);
                $data['salt'] = $salt;
                $data['login_time'] = $login_time;
            }
            $this->sitedb->data=$data;

            $this->sitedb->update("member");
            return $this->success('更新成功');

        }else{
            return $this->error('提交异常');
        }
    }


    /**
     * 验证码
     */
    public function verify() {
        return captcha();
    }
    /**
     * 退出登录
     */
    function logout() {
        session(null); // 清空当前的session
        return $this->success('退出成功',"",url('Index/index'));
    }
}
